package com.example.wjh.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.wjh.entity.LeaveApplicant;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface LeaveApplicantMapper extends BaseMapper<LeaveApplicant> {

    // 根据ID获取请假申请
    @Select("SELECT * FROM LeaveApplicants WHERE LeaveID = #{id}")
    LeaveApplicant selectById(@Param("id") Long id);

    // 插入新的请假申请
    @Insert("INSERT INTO LeaveApplicants (ApplicantName, ApplicantType, LeaveType, StartDate, EndDate, Reason, Status, CreatedAt) " +
            "VALUES (#{applicantName}, #{applicantType}, #{leaveType}, #{startDate}, #{endDate}, #{reason}, #{status}, #{createdAt})")
    int insertLeaveApplicant(LeaveApplicant leaveApplicant);

    // 更新请假申请
    @Update("UPDATE LeaveApplicants SET ApplicantName = #{applicantName}, ApplicantType = #{applicantType}, LeaveType = #{leaveType}, " +
            "StartDate = #{startDate}, EndDate = #{endDate}, Reason = #{reason}, Status = #{status}, CreatedAt = #{createdAt} " +
            "WHERE LeaveID = #{leaveId}")
    int updateLeaveApplicant(LeaveApplicant leaveApplicant);

    // 根据ID删除请假申请
    @Delete("DELETE FROM LeaveApplicants WHERE LeaveID = #{id}")
    int deleteById(@Param("id") Long id);

    // 获取所有请假申请
    @Select("SELECT * FROM LeaveApplicants")
    List<LeaveApplicant> getAllLeaveApplicants();

    //批量查询
    @Select("<script> " +
            "SELECT * FROM LeaveApplicants " +
            "<where> " +
            "<if test='applicantName != null'> AND ApplicantName LIKE CONCAT('%', #{applicantName}, '%')</if> " +
            "<if test='applicantType != null'> AND ApplicantType LIKE CONCAT('%', #{applicantType}, '%')</if> " +
            "<if test='leaveType != null'> AND LeaveType LIKE CONCAT('%', #{leaveType}, '%')</if> " +
            "<if test='status != null'> AND Status = #{status}</if> " +
            "</where> " +
            "</script>")
    List<LeaveApplicant> searchLeaveApplicants(
            @Param("applicantName") String applicantName,
            @Param("applicantType") String applicantType,
            @Param("leaveType") String leaveType,
            @Param("status") String status);
}